package com.example.demo.controller;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.example.demo.enity.CompanyInfo;
import com.example.demo.enity.JltdInfo;
import com.example.demo.enity.Msg;
import com.example.demo.enity.RecruitmentInfo;
import com.example.demo.enity.Resume;
import com.example.demo.service.BaseService;
import com.example.demo.service.UserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

@Controller
public class UserController {

	@Autowired
	UserService userService;
	@Autowired
	BaseService baseService;
	
	 /**
	   * 返回用户界面
	   */
	  @RequestMapping("/user/userpage") 
	  public String userpage() 
	  { 
		  return "userpage"; 
		  
	  }
	
	  /**
	   * 返回个人信息界面
	   */
	  @RequestMapping("/user/infopage") 
	  public String infopage() 
	  { 
		  return "infopage"; 
		  
	  }
	  
	  /**
	   * 1.返回用户的简历页
	   * @return
	   */
	  @RequestMapping("/user/resumepage") 
	  public String resumepage() 
	  { 
		  return "resumepage"; 
		  
	  }
	  
	  /**
	   * 1.返回用户的招聘页
	   * @return
	   */
	  @RequestMapping("/user/recruitmentpage") 
	  public String recruitmentpage() 
	  { 
		  return "recruitmentpage"; 
		  
	  }
	
	  /**
	   * 修改密码
	   */
	  @RequestMapping(value="/user/updatePassword",method = RequestMethod.POST)
	  @ResponseBody
	  public Msg updatePassword(String utel,String oldpassword,String newpassword) {
		 if(userService.loadUserByUsername(utel).getPassword().equals(oldpassword)) {
			 return Msg.success().add("updatePassword", userService.updatePassword(utel, newpassword));
		 }else {
			 return Msg.fail();
		 }
	  }
	  
	  /**
	   * 1.用户新建简历
	   * @param resume
	   * @return
	   */
	  @RequestMapping(value = "/user/insertresume",method = RequestMethod.POST) 
	  @ResponseBody
	  public Msg insertresume(Resume resume) 
	  { 
		 return  Msg.success().add("insertresume", userService.InsertResume(resume));
		 
	  }
	  
	  /**
	   * 根据用户的id查询所有简历，用于查看我的简历
	   * @param uid
	   * @return
	   */
	  @RequestMapping(value = "/user/selectResumeByUid",method = RequestMethod.POST) 
	  @ResponseBody
	  public Msg selectResumeByUid(@RequestParam(value = "pages",defaultValue = "1") int pages,@RequestParam Integer uid) {
		  
		  
		  if(userService.selectResumeByUid(uid).isEmpty()) {
			  return Msg.fail();
		  }else {
			  
			  PageHelper.startPage(pages, 5);
			  PageHelper.orderBy("Jlid asc");
			  List<Resume> resumes=userService.selectResumeByUid(uid);
			  PageInfo<Resume> pageinfo=new PageInfo<Resume>(resumes,5);
			  return Msg.success().add("resumepageInfos",pageinfo);
		  }
		 
		  
	  }
	  
	  /**
	   * 根据用户的id查询所有简历，用于投递简历
	   * @param uid
	   * @return
	   */
	  @RequestMapping(value = "/user/selectResumeCheckIfRegisterByUid",method = RequestMethod.POST) 
	  @ResponseBody
	  public Msg selectResumeCheckIfRegisterByUid( Integer uid) {
		  
		 if(userService.selectResumeByUid(uid).isEmpty()) {
			 return Msg.fail();
		 }
		
		 return Msg.success().add("selectResumeCheckIfRegisterByUid", userService.selectResumeByUid(uid));
			
	
	  }
	  

	  /**
	   * 根据简历的id查询简历
	   * @param jlid
	   * @return
	   */
	  @RequestMapping(value = "/user/selectOneResumeWithRecruitemntByJlid",method = RequestMethod.POST) 
	  @ResponseBody
	  public Msg selectOneResumeWithByUid(Integer jlid) {
		  
		 if(userService.selectOneResumeWithByUid(jlid)==null) {
			 return Msg.fail();
		 }else {
			 return Msg.success().add("selectOneResumeWithByUid", userService.selectOneResumeWithByUid(jlid));
		 }
		  
	  }
	  
	  /**
	   * 1.根据简历id删除简历
	   * @param jlid
	   * @return
	   */
	  @RequestMapping(value="/user/deleteResumeByPrimaryKey",method = RequestMethod.POST)
	  @ResponseBody
	  public Msg  deleteResumeByPrimaryKey(Integer jlid) {
		  if(userService.selectOneResumeWithByUid(jlid)==null) {
			  return Msg.fail();
		  }else {
			  return Msg.success().add("deleteResumeByPrimaryKey", userService.deleteResumeByPrimaryKey(jlid));
		  }
	  }
	  
	  
	  /**
	   * 1.招聘者根据自己的id查询其他用户投递的简历信息
	   */
	  @RequestMapping("/user/selectResumeWithRecruitmentByZUid")
	  @ResponseBody
	  public Msg selectResumeWithRecruitmentByZUid(@RequestParam(value="pages",defaultValue = "1") int pages,@RequestParam Integer uid){

		  if(userService.selectResumeWithRecruitmentByZUid(uid).isEmpty()) {
			  return Msg.fail();
		  }else {
			  PageHelper.startPage(pages,5);
			  PageHelper.orderBy("time desc");
			  List<Resume> resume=userService.selectResumeWithRecruitmentByZUid(uid);
			  PageInfo<Resume> resuInfos=new PageInfo<Resume>(resume, 5);
			return Msg.success().add("selectResumeWithRecruitmentByZUid", resuInfos);
		}
			 
	  }
	  
	  /**
	   * 1.求职者根据自己的id查询投递的简历信息
	   */
	  @RequestMapping("/user/selectResumeWithRecruitmentByTUid")
	  @ResponseBody
	  public Msg selectResumeWithRecruitmentByTUid(@RequestParam(value="pages",defaultValue = "1") int pages,@RequestParam Integer uid){

		  if(userService.selectResumeWithRecruitmentByTUid(uid).isEmpty()) {
			  return Msg.fail();
		  }else {
			  PageHelper.startPage(pages,5);
			  PageHelper.orderBy("time desc");
			  List<Resume> resume=userService.selectResumeWithRecruitmentByTUid(uid);
			  PageInfo<Resume> resuInfos=new PageInfo<Resume>(resume, 5);
			return Msg.success().add("selectResumeWithRecruitmentByTUid", resuInfos);
		}
			 
	  }
	  
	  /**
	   * 
	   * 根据用户插入新的招聘信息
	   * @param uid
	   * @return
	   */
	  @RequestMapping(value = "/user/insertRecruitmentInfo",method = RequestMethod.POST) 
	  @ResponseBody
	  public Msg insertRecruitmentInfo(RecruitmentInfo recruitmentinfo) {
		  recruitmentinfo.setTime(new Date());
		  return Msg.success().add("insertRecruitmentInfo", userService.InsertRecruitment(recruitmentinfo));
	  }
	  
	  /**
	   *招聘者id查询自己的招聘信息
	   * @param uid
	   * @return
	   */
	  @RequestMapping("/user/selectRecruitmentInfoByUid") 
	  @ResponseBody
	  public Msg selectRecruitmentInfoByUid(@RequestParam Integer uid,@RequestParam(value = "pages",defaultValue = "1") int pages) 
	  { 
		  if(userService.selectRecruitmentInfoByUid(uid).isEmpty()) {
			  return Msg.fail();
		  }else {
		  PageHelper.startPage(pages, 5);
		  PageHelper.orderBy("zpid asc");
		  List<RecruitmentInfo> recruitmentInfos=userService.selectRecruitmentInfoByUid(uid);
		  PageInfo<RecruitmentInfo> page=new PageInfo<>(recruitmentInfos);
		  return  Msg.success().add("RecruitmentPageInfo",page);
	  }
	  }
	  
	  
	  /**
	   * 招聘者填写公司信息
	   * @param companyInfo
	   * @return
	   */
	  @RequestMapping("/user/insertUserCompany") 
	  @ResponseBody
	  public Msg insertUserCompany(CompanyInfo companyInfo) {
		  if(userService.selectCompanyByUid(companyInfo.getUcid())==null) {
		  return Msg.success().add("insertUserCompany", userService.insertUserCompany(companyInfo));
	  }else {
		
		  return Msg.fail();
	}
  }
	  
	  /**
	   * 查看招聘者公司信息
	   * @param uid
	   * @return
	   */
	  @RequestMapping("/user/selectCompanyByUid") 
	  @ResponseBody
	  public Msg selectCompanyByUid(Integer ucid) {
		  if(userService.selectCompanyByUid(ucid)!=null) {
		  return Msg.success().add("selectCompanyByUid", userService.selectCompanyByUid(ucid));
		  }else {
			  return Msg.fail();
		  }
	  }
	  
	  /**
	   * 1.简历投递
	   */
	 @RequestMapping(value="/user/insertJlSelective",method =RequestMethod.POST) 
	 @ResponseBody
	 public Msg insertJlSelective(@RequestParam int tuid,@RequestParam int tzid,@RequestParam int zpid,@RequestParam int jlid) {
		 
		 
		 if(userService.ckeckLjtd(tuid, zpid)==null) {
			 JltdInfo record=new JltdInfo();
			 record.setChecked(false);
			 record.setJlid(jlid);
			 record.setTime(new Date());
			 record.setTuid(tuid);
			 record.setTzid(tzid);
			 record.setZpid(zpid);
		 return Msg.success().add("insertJlSelective", userService.insertJlSelective(record));
	 }else {
		
		 return Msg.fail();
	}
	 }
	 
	 
	 /**
	  * 1.招聘者查看投递信息，然后设置该条投递信息已经被查看
	  */
	 
	@RequestMapping(value="/user/updateJltdIfCheckedByPrimaryKey",method =RequestMethod.POST) 
	@ResponseBody
	public Msg updateJltdIfCheckedByPrimaryKey(Integer tid) {
		
	    System.out.println(tid);
		userService.updateJltdIfCheckedByPrimaryKey(tid);
		System.out.println("nihao");
		return Msg.success();
	}
	
	/**
	 * 
	 * 1.求职者提交举报招聘信息的记录
	 * @param zpid
	 * @param ruid
	 * @return
	 */
	@RequestMapping(value="/user/reportRecruitmentWithUserByZpid",method =RequestMethod.POST) 
	@ResponseBody
	public Msg reportRecruitmentWithUserByZpid(Integer zpid,Integer ruid) {
		if(userService.selectWithZpidAndRuid(zpid, ruid)) {
			return Msg.success().add("reportRecruitmentWithUserByZpid", userService.reportRecruitmentWithUserByZpid(zpid));
		}else {
			return Msg.fail();
		}
		
	}
}
